//http://www.taufanaditya.com/bootstrap-markdown/
(function ($) {
    var root = daicuo.config.root;
    daicuo.ajax.script([
    root+'apps/markdown/static/bootstrap-markdown.min.js',
    root+'apps/markdown/static/hyperdown.min.js'],
    function(){
        //加载CSS
        daicuo.ajax.css(root+'apps/markdown/static/bootstrap-markdown.css');
        //解析器
        var parser = new HyperDown();
        window.marked = function (text) {
            return parser.makeHtml(text);
        };
        //定义语言
        $.fn.markdown.messages.zh = {
            'Bold': "粗体",
            'Italic': "斜体",
            'Heading': "标题",
            'URL/Link': "链接",
            'Image': "图片",
            'List': "列表",
            'Unordered List': "无序列表",
            'Ordered List': "有序列表",
            'Code': "代码",
            'Quote': "引用",
            'Preview': "预览",
            'strong text': "粗体",
            'emphasized text': "强调",
            'heading text': "标题",
            'enter link description here': "输入链接说明",
            'Insert Hyperlink': "URL地址",
            'enter image description here': "输入图片说明",
            'Insert Image Hyperlink': "图片URL地址",
            'enter image title here': "在这里输入图片标题",
            'list text here': "这里是列表文本",
            'code text here': "这里输入代码",
            'quote here': "这里输入引用文本"
        };
        //调用MarkDown
        $('.dc-editor').markdown({
            resize: 'vertical',
            language: 'zh',
            iconlibrary: 'fa',
            autofocus: false,
            savable: false,
            onShow: function(e){
                daicuo.upload.init({
                    element: '[data-handler="bootstrap-markdown-cmdUploadImage"]',
                    container: 'dc-editor-upload',//$('.dc-editor').parent().get(0),
                    multiple: true,
                    onSuccess: function(up, file, xhr){
                        e.replaceSelection("\n" + '!['+file.responseTp.data.old_name+']('+file.responseTp.data.url+' "在这里输入图片标题")');
                    }
                });
            },
            additionalButtons: [
                [{
                    name: "groupCustom",
                    data: [{
                        name: "cmdUploadImage",
                        toggle: false,
                        title: "上传",
                        icon: "fa fa-file-image-o",
                        callback: function (e) {
                            return false;
                        }
                    }, {
                        name: "cmdSelectAttachment",
                        toggle: false,
                        title: "选择附件",
                        icon: "fa fa-file",
                        callback: function (e) {
                            return false;
                        }
                    }]
                }]
            ]
        });
    });
})(jQuery);